<?php
/**
 * 项目报告数据
 * @author qianxun
 *
 */
class proReportModel extends MY_Model{
    function proReportModel(){
        parent::MY_Model();
        $this->table = 'ProReport';
    }
    /**
     * 设置是否报表更新状态，若更新状态，在读取报表数据时重新计算报表数据
     * @param number $proId
     * @return boolean
     */
    function setHavaNew($proId){
        $findData = $this->getData(array('proId'=>$proId));
        if ($findData){
            $this->edit(array('proId'=>$proId), array('haveNew'=>1));
        }else{
            $this->add(array('proId'=>$proId,'haveNew'=>1,'reportData'=>serialize(array()),'updateTime'=>time(),'createTime'=>time()));
        }
        return true;
    }
    /**
     * 获取绑定了专营店相关数据的项目列表
     * @param array $where
     */
    function getStoreProList($proId){
        //获取制定项目的动作列表
        $this->load->model('proActItemModel');
        $actItemList = $this->proActItemModel->getActItem($proId);
        $this->load->model('storeProModel');
        $storeProList = $this->storeProModel->getNewList(array('storeId != ' => 0,'proId'=>$proId,'auditRs >' => 0));
        //新的项目列表
        foreach ($storeProList as $k => $v){
            //题目列表
            $v['itemList'] = $actItemList;
            foreach ($v['itemList'] as $itemK => $itemV){
                if(isset($v['itemValue'][$itemK])){
                    $itemV += $v['itemValue'][$itemK];
                    $v['itemList'][$itemK] = $itemV;
                }
            }
            unset($v['itemValue']);
            $storeProList[$k] = $v;
        }
        return $storeProList;
    }
    /**
     * 统计报表数据
     * @param number $proId
     * @return array
     */
    function countReportData($proId){
        $data = array(
            'validCount' => 0,//有效数
            'invalidCount' => 0,//无效数
            'countDetail' => array(),//
        );
        //专营店答题列表
        $storeProList = $this->getStoreProList($proId);
        //统计明细
        $countDetail = array();
        foreach ($storeProList as $v){
            if (in_array($v['auditRs'], array(1,3))){//反馈有效(合格、部分合格)
                //数量统计
                $data['validCount'] ++;
                //扫描题目列表
                foreach ($v['itemList'] as $itemK => $itemV){
                    $actId = (int)$itemV['actId'];
                    //动作审核状态
                    $proActAuditRs = isset($v['auditValue'][$actId]['auditRs']) ? (int)$v['auditValue'][$actId]['auditRs'] : 0;
                    
                    isset($countDetail[$itemK]) || $countDetail[$itemK] = array(
                        'auditRsCount' => array(1 => 0, 2 => 0, -1 => 0),//审核状态数统计
                        'totalScore' => 0,//总分(数据题)
                        'optionsCount' => array(),//单选和多选的选项数量统计
                        'timeCount' => array(),//时间题数量统计
                        );
                    //各审核状态统计数
                    $countDetail[$itemK]['auditRsCount'][(int)$proActAuditRs] ++;
                    //分别统计
                    if (in_array($itemV['type'], array(2))){//平均分
                        $countDetail[$itemK]['totalScore'] += $itemV['value'];
                    }elseif (in_array($itemV['type'], array(3,4)) && $proActAuditRs == 1){//选项数
                        //初始化数量
                        if (empty($countDetail[$itemK]['optionsCount'])){
                            foreach ((array)$itemV['otherData']['options'] as $optionK => $optionV){
                                $countDetail[$itemK]['optionsCount'][$optionK] = 0;
                            }
                        }
                        //统计项目总数
                        foreach ((array)$itemV['value'] as $valueV){
                            $countDetail[$itemK]['optionsCount'][(int)$valueV] ++;
                        }
                    }elseif (in_array($itemV['type'], array(5)) && $proActAuditRs == 1){//时间统计数
                        //isset($countDetail[$itemK]['timeCount'][$itemV['value']]) || $countDetail[$itemK]['timeCount'][$itemV['value']] = 0;
                        $countDetail[$itemK]['timeCount'][$v['storeId']] = $itemV['value'];
                    }
                }
            }elseif (in_array($v['auditRs'], array(2))){//反馈无效(不合格)
                //数量统计
                $data['invalidCount'] ++;
            }
        }
        $data['countDetail'] = $countDetail;
        return $data;
    }
    /**
     * 读取报表数据
     * @param array $proId
     */
    function getReportData($proId){
        $findData = $this->getData(array('proId'=>$proId));
        if ($findData && $findData['haveNew'] == 0){
            return unserialize($findData['reportData']);
        }
        $reportData = $this->countReportData($proId);
        $data = array(
            'haveNew' => 0,
            'reportData'=>serialize($reportData),
            'updateTime'=>time(),
        );
        if ($findData){
            $this->edit(array('proId'=>$proId), $data);
        }else{
            $data['proId'] = $proId;
            $data['createTime'] = time();
            $this->add($data);
        }
        return $reportData;
    }
}